package com.mmall.concurrency.example.threadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mmall.concurrency.example.atomic.AtomicExample3;

@Slf4j
public class ThreadPoolExample4 {
	private static Logger log = LoggerFactory.getLogger(AtomicExample3.class);

    public static void main(String[] args) {

        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);

        for (int i = 0; i < 10; i++) {
            final int index = i;
            executorService.schedule(new Runnable() {
                @Override
                public void run() {
                    log.info("Thread: {} ,task: {}", Thread.currentThread().getName(), index);
                }
            }, 3, TimeUnit.SECONDS);
        }

        executorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                log.warn("scheduleAtFixedRate");
            }
        },1, 3, TimeUnit.SECONDS); // 每隔三秒执行一次， 下面shutdown()方法不要
//        executorService.shutdown();

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                log.warn("timer run");
            }
        }, new Date(), 5*1000);
    }
}
